{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# File\n",
    "\n",
    "本文档描述与 File 相关的接口。请注意，此处的 File 不仅用于 Assistants 场景。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 环境准备\n",
    "\n",
    "首先需要安装AppBuilder-SDK代码库，若已在开发环境安装，则可跳过此步。\n",
    "\n",
    "**注意：**: appbuilder-sdk 的python版本要求 3.9+，安装的SDK version >= 0.7.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "!python3 -m pip install appbuilder-sdk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import appbuilder\n",
    "\n",
    "# 配置你的密钥，主要在这之前需要首先申请Assistant API的内测资格\n",
    "os.environ[\"APPBUILDER_TOKEN\"] = \"your_appbuilder_token\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## File相关函数\n",
    "\n",
    "### 上传文件\n",
    "\n",
    "#### 功能介绍\n",
    "\n",
    "上传文件，返回file_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id='file-555044143673413' bytes=96130 object='file' purpose='assistant' create_at=1717645226 filename='test01.png' classification_id='42685563'\n"
     ]
    }
   ],
   "source": [
    "file_path = \"./data/test01.png\"\n",
    "file = appbuilder.assistant.assistants.files.create(file_path=file_path)\n",
    "\n",
    "# 打印上传的file相关信息\n",
    "print(file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查询已上传的文件列表\n",
    "\n",
    "#### 功能介绍\n",
    "\n",
    "查询文件列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id='file-537384975425605' bytes=20 object='file' purpose='' censored=<AuditStatus.APPROVED: 1> create_at=1713333906 filename='test.txt' classification_id='42685563' file_type='document'\n",
      "id='file-537456861585477' bytes=12696269 object='file' purpose='' censored=<AuditStatus.APPROVED: 1> create_at=1713351456 filename='2.1M_.png' classification_id='42685563' file_type='image'\n",
      "id='file-537457096659013' bytes=12696269 object='file' purpose='' censored=<AuditStatus.APPROVED: 1> create_at=1713351513 filename='2.1M_.png' classification_id='42685563' file_type='image'\n",
      "id='file-551587052920901' bytes=96130 object='file' purpose='assistant' censored=<AuditStatus.APPROVED: 1> create_at=1716801210 filename='qa_doc_parser_extract_table_from_doc.png' classification_id='42685563' file_type='image'\n",
      "id='file-551587093069893' bytes=96130 object='file' purpose='assistant' censored=<AuditStatus.APPROVED: 1> create_at=1716801219 filename='qa_doc_parser_extract_table_from_doc.png' classification_id='42685563' file_type='image'\n",
      "id='file-551587117764677' bytes=96130 object='file' purpose='assistant' censored=<AuditStatus.APPROVED: 1> create_at=1716801225 filename='qa_doc_parser_extract_table_from_doc.png' classification_id='42685563' file_type='image'\n",
      "id='file-551587412295749' bytes=96130 object='file' purpose='assistant' censored=<AuditStatus.APPROVED: 1> create_at=1716801297 filename='qa_doc_parser_extract_table_from_doc.png' classification_id='42685563' file_type='image'\n",
      "id='file-551587506688069' bytes=96130 object='file' purpose='assistant' censored=<AuditStatus.APPROVED: 1> create_at=1716801320 filename='qa_doc_parser_extract_table_from_doc.png' classification_id='42685563' file_type='image'\n",
      "id='file-551587515215941' bytes=96130 object='file' purpose='assistant' censored=<AuditStatus.APPROVED: 1> create_at=1716801322 filename='qa_doc_parser_extract_table_from_doc.png' classification_id='42685563' file_type='image'\n",
      "id='file-551587685650501' bytes=96130 object='file' purpose='assistant' censored=<AuditStatus.APPROVED: 1> create_at=1716801364 filename='qa_doc_parser_extract_table_from_doc.png' classification_id='42685563' file_type='image'\n",
      "id='file-551587701821509' bytes=96130 object='file' purpose='assistant' censored=<AuditStatus.APPROVED: 1> create_at=1716801368 filename='qa_doc_parser_extract_table_from_doc.png' classification_id='42685563' file_type='image'\n"
     ]
    }
   ],
   "source": [
    "files_list = appbuilder.assistant.assistants.files.list()\n",
    "\n",
    "# 打印已经上传的file列表与相关信息\n",
    "# 这里仅=展示前10条\n",
    "num=0\n",
    "for file_data in files_list.data:\n",
    "    print(file_data)\n",
    "    num+=1\n",
    "    if num >10:\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查询已上传的文件信息\n",
    "\n",
    "#### 功能介绍\n",
    "\n",
    "根据file_id查询文件信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id='file-555044143673413' bytes=96130 object='file' purpose='assistant' censored=<AuditStatus.APPROVED: 1> create_at=1717645226 filename='test01.png' classification_id='42685563' file_type='image'\n"
     ]
    }
   ],
   "source": [
    "files_query = appbuilder.assistant.assistants.files.query(file_id=file.id)\n",
    "\n",
    "# 打印依据file_id查询到的文件信息\n",
    "print(files_query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 下载文件\n",
    "\n",
    "#### 功能介绍\n",
    "\n",
    "根据file_id下载一个已上传的文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 依据file_id将文件下载到目标文件目录\n",
    "appbuilder.assistant.assistants.files.download(file_id=file.id, file_path=\"./data/\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查看文件内容\n",
    "\n",
    "#### 功能介绍\n",
    "\n",
    "查看文件内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "files_content=appbuilder.assistant.assistants.files.content(file_id=file.id)\n",
    "\n",
    "# 打印文件的数据类型与内容\n",
    "print(files_content.content_type)\n",
    "print(files_content.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 删除文件\n",
    "\n",
    "#### 功能介绍\n",
    "\n",
    "根据file_id删除一个已上传的文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 根据file_id删除一个已上传的文件\n",
    "files_delete = appbuilder.assistant.assistants.files.delete(file_id=file.id)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
